package com.homeparking.manage.service.impl;

import java.util.List;
import com.homeparking.common.exception.ServiceException;
import com.homeparking.common.utils.DateUtils;
import com.homeparking.common.utils.SecurityUtils;
import com.homeparking.common.utils.StringUtils;
import com.homeparking.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.homeparking.manage.mapper.ManageCommunityMapper;
import com.homeparking.manage.domain.ManageCommunity;
import com.homeparking.manage.service.IManageCommunityService;

/**
 * 小区信息Service业务层处理
 * 
 * @author liuyangyang
 * @date 2025-10-23
 */
@Service
public class ManageCommunityServiceImpl implements IManageCommunityService {
    @Autowired
    private ManageCommunityMapper manageCommunityMapper;

    /**
     * 拉取小区信息
     *
     * @param manageCommunity 小区信息
     * @return 结果
     */
    @Override
    public int pullManageCommunity(List<ManageCommunity> manageCommunity) {

        return manageCommunityMapper.pullManageCommunity(manageCommunity);
    }

    /**
     * 查询小区信息
     *
     * @param communityId 小区信息主键
     * @return 小区信息
     */
    @Override
    public ManageCommunity selectManageCommunityByCommunityId(String communityId) {
        return manageCommunityMapper.selectManageCommunityByCommunityId(communityId);
    }

    /**
     * 查询小区信息列表
     *
     * @param manageCommunity 小区信息
     * @return 小区信息
     */
    @Override
    public List<ManageCommunity> selectManageCommunityList(ManageCommunity manageCommunity) {
        return manageCommunityMapper.selectManageCommunityList(manageCommunity);
    }

    /**
     * 新增小区信息
     *
     * @param manageCommunity 小区信息
     * @return 结果
     */
    @Override
    public int insertManageCommunity(ManageCommunity manageCommunity) {
        manageCommunity.setCreateTime(DateUtils.getNowDate());
        return manageCommunityMapper.insertManageCommunity(manageCommunity);
    }

    /**
     * 修改小区信息
     *
     * @param manageCommunity 小区信息
     * @return 结果
     */
    @Override
    public int updateManageCommunity(ManageCommunity manageCommunity) {
        manageCommunity.setUpdateTime(DateUtils.getNowDate());
        return manageCommunityMapper.updateManageCommunity(manageCommunity);
    }

    /**
     * 批量删除小区信息
     *
     * @param communityIds 需要删除的小区信息主键
     * @return 结果
     */
    @Override
    public int deleteManageCommunityByCommunityIds(String[] communityIds) {
        return manageCommunityMapper.deleteManageCommunityByCommunityIds(communityIds);
    }

    /**
     * 删除小区信息信息
     *
     * @param communityId 小区信息主键
     * @return 结果
     */
    @Override
    public int deleteManageCommunityByCommunityId(String communityId) {
        return manageCommunityMapper.deleteManageCommunityByCommunityId(communityId);
    }

    /**
     * 导入用户数据
     *
     * @param communityList 用户数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importCommunity(List<ManageCommunity> communityList, Boolean updateSupport) {
        if (StringUtils.isNull(communityList) || communityList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (ManageCommunity community : communityList) {
            try {
                // 验证学生数据是否已经存在
                ManageCommunity s = manageCommunityMapper.selectCommunityName(community.getCommunityName());
                if (StringUtils.isNull(s)) {

                    manageCommunityMapper.insertManageCommunity(community);
                    successNum++;
                } else if (updateSupport) {

                    manageCommunityMapper.updateManageCommunity(community);
                    successNum++;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、学生 " + community.getCommunityName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、学生 " + community.getCommunityName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
